package main

import (
	"context"
	"demo/cfg"
	"demo/log"
	"demo/resource"
	router "demo/routers"
	_ "demo/utils"
	"fmt"
	"net/http"
	"os"
	"os/signal"
	"time"

	"github.com/gin-gonic/gin"
)

// Router _
var Router *gin.Engine

func main() {
	handler := router.InitRouter()

	server := &http.Server{
		Addr:    ":8899",
		Handler: handler,
	}

	go func() {
		if err := server.ListenAndServe(); err != nil {
			fmt.Printf("Listen: %s\n", err)
		}
	}()

	quitChan := make(chan os.Signal)
	signal.Notify(quitChan, os.Interrupt)
	<-quitChan // 捕获到信号则执行

	fmt.Println("shutting down server")
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	if err := server.Shutdown(ctx); err != nil {
		fmt.Printf("err shutdown server: %v", err)
	}
	fmt.Println("server existing")
}

func init() {
	gin.SetMode(gin.ReleaseMode)
	cfg.InitConfig()
	log.InitLog()
	resource.InitDB()
}
